﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MIC_LOGIC;
using System.Threading;

namespace MIC_GUI.ViewModel
{
    /// <summary>
    /// Converts incoming data 
    /// </summary>
    public class SerialDataConverter
    {
        #region private members
        private ILogicFacade _logic;
        private double[] _convertedValues;
        #endregion

        #region properties
        /// <summary>
        /// Get last converted Accelerometer value
        /// </summary>
        public double AccelerometerAngle { get { return _convertedValues[0]; } }

        /// <summary>
        /// Get last converted Gyro value
        /// </summary>
        public double GyroAnglePrSec { get { return _convertedValues[1]; } }

        /// <summary>
        /// Get last converted Angle value
        /// </summary>
        public double ApproximatedAngle { get { return _convertedValues[2]; } }

        #endregion

        #region events
        /// <summary>
        /// Occurs on new data converted
        /// </summary>
        public event EventHandler OnDataValuesConverted;

        /// <summary>
        /// Occurs on error message
        /// </summary>
        public event StringDelegate OnError;
        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="logic">Instance of the ILogicFacade in use</param>
        public SerialDataConverter(ILogicFacade logic)
        {
            _convertedValues = new double[5];
           
            _logic = logic;
            _logic.OnInputDataStreamChanged += new StringDelegate(_logic_OnInputDataStreamChanged);

        }

        /// <summary>
        /// New input data received event handler
        /// </summary>
        /// <param name="msg">new data</param>
        private void _logic_OnInputDataStreamChanged(string msg)
        {
            msg = msg.Trim();
            string[] temp = msg.Split(',');

            try
            {
                _convertedValues[0] = ((double)(Int32.Parse(temp[0])) / 100);
                _convertedValues[1] = ((double)(Int32.Parse(temp[1])) / 100);
                _convertedValues[2] = ((double)(Int32.Parse(temp[2])) / 100);

                if (_convertedValues[2] > 180)
                    _convertedValues[2] = -180;

                if (_convertedValues[2] < -180)
                    _convertedValues[2] = 180;

                if (_convertedValues[0] > 180)
                    _convertedValues[0] = -180;

                if (_convertedValues[0] < -180)
                    _convertedValues[0] = 180;

                if (OnDataValuesConverted != null)
                    OnDataValuesConverted(this, null);

            }
            catch (Exception ex)
            {
                if (OnError != null)
                    OnError("Error converting data: " + ex.Message);
            }
        }
    }
}
